
import os
import json
from glob import glob, iglob


ALL_dir = r"E:\xianhuang0818\xhsi_seg20201226"
INPUT_DIR = os.path.join(ALL_dir, "json_702")
OUTPUT_DIR = os.path.join(ALL_dir, "json")
# ALL_dir = r"E:\xianhuang0818\xhsi_seg20210113"
# INPUT_DIR = os.path.join(ALL_dir, "json")
# OUTPUT_DIR = os.path.join(ALL_dir, "labelme_py")

idx_i = 0
for file in iglob(INPUT_DIR + '/*.json'):
    print(file)
    f = open(file, 'r', encoding='utf-8')
    j_name = file.split("\\")[-1]
    ts = j_name.split(".")
    title = ts[0]
    title_sp = title.split("_")
    idx = title_sp[-1]
    # idx_i = int(idx)

    title_ap = ""
    for kkk in range(0, len(title_sp) - 1):
        title_ap += title_sp[kkk] + "_"

    """ Load json files """
    data = json.load(f)
    new_name = "%s%04d.json"%(title_ap, 2000 + idx_i)
    save_path = os.path.join(OUTPUT_DIR, new_name)
    print(save_path)
    json.dump(data,open(save_path,'w'))
    f.close()
    idx_i += 1